{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 设置索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "   a  b    c  d\n0  0  7  one  h\n1  1  6  one  j\n2  2  5  one  k\n3  3  4  two  l\n4  4  3  two  m\n5  5  2  two  n\n6  6  1  two  o",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>7</td>\n      <td>one</td>\n      <td>h</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>6</td>\n      <td>one</td>\n      <td>j</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>5</td>\n      <td>one</td>\n      <td>k</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>4</td>\n      <td>two</td>\n      <td>l</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>3</td>\n      <td>two</td>\n      <td>m</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>5</td>\n      <td>2</td>\n      <td>two</td>\n      <td>n</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>6</td>\n      <td>1</td>\n      <td>two</td>\n      <td>o</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#索引中单项不可变，但是整体可以换掉\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "a = pd.DataFrame({'a': range(7),'b': range(7, 0, -1),\n",
    "                  'c': ['one','one','one','two','two','two', 'two'],\n",
    "                  'd': list(\"hjklmno\")})\n",
    "a"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-07T03:51:18.364541200Z",
     "start_time": "2024-05-07T03:51:17.857039300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 给a换个行索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a  b    c  d\n",
      "0  0  7  one  h\n",
      "1  1  6  one  j\n",
      "2  2  5  one  k\n",
      "3  3  4  two  l\n",
      "4  4  3  two  m\n",
      "5  5  2  two  n\n",
      "6  6  1  two  o\n",
      "--------------------------------------------------\n",
      "   a  b    c  d\n",
      "a  0  7  one  h\n",
      "b  1  6  one  j\n",
      "c  2  5  one  k\n",
      "d  3  4  two  l\n",
      "e  4  3  two  m\n",
      "f  5  2  two  n\n",
      "g  6  1  two  o\n"
     ]
    }
   ],
   "source": [
    "c=a.copy()\n",
    "a.index=list('abcdefg')  #a的索引变了，a.index更换索引\n",
    "print(c)\n",
    "print('-'*50)\n",
    "\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-07T03:51:18.398956400Z",
     "start_time": "2024-05-07T03:51:18.369858200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 怎么知道df有多少个样本？"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "(7, 4)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.values.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T03:51:18.435074600Z",
     "start_time": "2024-05-07T03:51:18.382949600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "   a  b    c  d\n",
      "0  0  7  one  h\n",
      "1  1  6  one  j\n",
      "2  2  5  one  k\n",
      "3  3  4  two  l\n",
      "4  4  3  two  m\n",
      "5  5  2  two  n\n",
      "6  6  1  two  o\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": "    a   b    c    d\na NaN NaN  NaN  NaN\nb NaN NaN  NaN  NaN\nc NaN NaN  NaN  NaN\nd NaN NaN  NaN  NaN\ne NaN NaN  NaN  NaN\nf NaN NaN  NaN  NaN\ng NaN NaN  NaN  NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>a</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>b</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>c</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>d</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>e</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>f</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>g</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('-'*50)\n",
    "b=c.reindex(list('abcdefg'))  #返回一个新的df，索引是设置了c的索引后，c索引不变,b是没有值\n",
    "print(c)\n",
    "print('-'*50)\n",
    "b"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-07T03:51:18.479332200Z",
     "start_time": "2024-05-07T03:51:18.397630300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 让某些列变为索引，比如让c列，d列数据变为索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a  b    c  d\n",
      "a  0  7  one  h\n",
      "b  1  6  one  j\n",
      "c  2  5  one  k\n",
      "d  3  4  two  l\n",
      "e  4  3  two  m\n",
      "f  5  2  two  n\n",
      "g  6  1  two  o\n",
      "--------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": "       a  b\nc   d      \none h  0  7\n    j  1  6\n    k  2  5\ntwo l  3  4\n    m  4  3\n    n  5  2\n    o  6  1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n    </tr>\n    <tr>\n      <th>c</th>\n      <th>d</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">one</th>\n      <th>h</th>\n      <td>0</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>j</th>\n      <td>1</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>k</th>\n      <td>2</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th rowspan=\"4\" valign=\"top\">two</th>\n      <th>l</th>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>m</th>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>n</th>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>o</th>\n      <td>6</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d=a.copy()\n",
    "print(d)\n",
    "print('-'*50)\n",
    "d.set_index(['c','d'],inplace=True) # 把c、d列数据变为索引，inplace=True表示在原df上修改，原来的行索引直接丢掉\n",
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2024-05-07T03:51:18.539284800Z",
     "start_time": "2024-05-07T03:51:18.409199300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 按位置重新设置行索引\n",
    "### 原来的分组索引会被拆开，但保留（如果是s则保留行索引，变为df），然后按位置生成行索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "data_user = pd.DataFrame({'user_id': [1, 1, 1, 2, 2, 2, 3, 3, 3],\n",
    "                          'date': ['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-01', '2020-01-02', '2020-01-03', '2020-01-01', '2020-01-02', '2020-01-03'],\n",
    "                          'behavior_type': ['4', '2', '4', '4', '1', '4', '3', '4', '4']})\n",
    "data_user_buy1= data_user[data_user.behavior_type =='4' ].groupby(['date' , 'user_id']).count()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T03:53:04.704805500Z",
     "start_time": "2024-05-07T03:53:04.624034100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "                    behavior_type\ndate       user_id               \n2020-01-01 1                    1\n           2                    1\n2020-01-02 3                    1\n2020-01-03 1                    1\n           2                    1\n           3                    1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>behavior_type</th>\n    </tr>\n    <tr>\n      <th>date</th>\n      <th>user_id</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2020-01-01</th>\n      <th>1</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2020-01-02</th>\n      <th>3</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2020-01-03</th>\n      <th>1</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_user_buy1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T03:53:05.847489100Z",
     "start_time": "2024-05-07T03:53:05.789812100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "         date  user_id  behavior_type\n0  2020-01-01        1              1\n1  2020-01-01        2              1\n2  2020-01-02        3              1\n3  2020-01-03        1              1\n4  2020-01-03        2              1\n5  2020-01-03        3              1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>date</th>\n      <th>user_id</th>\n      <th>behavior_type</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2020-01-01</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2020-01-01</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2020-01-02</td>\n      <td>3</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2020-01-03</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2020-01-03</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2020-01-03</td>\n      <td>3</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_user_buy1['behavior_type'].reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T03:53:07.437566Z",
     "start_time": "2024-05-07T03:53:07.390665300Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
